home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / oper_sys / emerald / emrldsys.lha / Kernel / MsgOps / handlerCode.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-08-17  |  9.3 KB  |  247 lines

  1. /* File: /u1/Kernel/MsgOps/handlerCode.c */
  2.  
  3. /*
  4.  * $Header:   /u1/Eden/Source/MsgOps/RCS/HandlerCode.v  Revision 1.12  84/11/28  23:03:31  schwartz  Exp$
  5.  * INTERFACE:    Defined by the exported procedures.
  6.  *
  7.  * FUNCTION:    Provides the Message Module Message Handler
  8.  *              Services.
  9.  *
  10.  * IMPORTS:    Eden/Source/MsgOps/Types.h,
  11.  *              /u1/Eden/Source/MsgOps/MsgTypes.h,
  12.  *              /u1/Eden/ErrCodes/MMcodes.h.
  13.  *
  14.  * EXPORTS:    MMDefineMsgHandler, MMDispatchMsg.
  15.  *              MMDGHandler, MMDefineDGHandler.
  16.  *
  17.  * DESIGN:      The message handler routines maintain a static
  18.  *              two-dimensional array indexed by message type and
  19.  *              subtype and containing the associated disassembled
  20.  *              message handler.  Eventually,
  21.  *              this design will be modified to implement a more
  22.  *              dynamic (and possibly more sparse) data structure
  23.  *              in order to be more efficient.
  24.  *
  25.  * $Log:    /u1/Eden/Source/MsgOps/RCS/HandlerCode.v $
  26.  * Revision 1.12  84/11/28  23:03:31  schwartz
  27.  * Changed MMDispatchMsg() to never call MMDeallocateMsg(), since this
  28.  * is done by all MM routines that call it after the unsuccessful return.
  29.  *
  30.  * Revision 1.11  83/10/14  16:37:57  mager
  31.  * Removed Datagram level of Message Module.  Increased size of message to just
  32.  * under 1K. Changed calls to malloc and free with xalloc and xfree.
  33.  * Removed redundant setting of ipc header.
  34.  * Reduced initialization of messages in MMAllocateMsg.
  35.  * 
  36.  * Revision 1.9  83/05/19  20:14:02  cady
  37.  * Fixed MMDefineDGHandler bug.
  38.  * 
  39.  * Revision 1.8  83/03/18  09:59:01  cady
  40.  * Added feature to allow NULL oldhandler pointer.
  41.  * 
  42.  * Revision 1.7  83/02/26  20:38:37  cady
  43.  * Fixed trace levels.
  44.  * 
  45.  * Revision 1.6  83/02/25  12:16:27  cady
  46.  * Added new trace levels.
  47.  * 
  48.  * Revision 1.5  83/02/24  16:40:13  cady
  49.  * Replaced conditional debug trace with dynamic trace.
  50.  * Added conditional Kernel compilation.
  51.  * 
  52.  * Revision 1.4  83/02/22  12:05:58  cady
  53.  * Replaced #if Debug with #ifdef Debug.
  54.  * 
  55.  * Revision 1.3  83/01/31  12:36:47  cady
  56.  * Fixed bug introduced by previous bug fix.
  57.  * 
  58.  * Revision 1.2  83/01/29  14:44:55  cady
  59.  * Updated message subtypes to start at 0.
  60.  * 
  61.  * Revision 1.1  83/01/06  14:09:55  cady
  62.  * Initial revision
  63.  * 
  64.  * 13-Dec-1982    Initial implementation. S. Banawan.
  65.  */
  66.  
  67. /****************************************************************/
  68. /*              Message Module Handler Library                  */
  69. /****************************************************************/
  70.  
  71. #include "Kernel/h/mmCodes.h"
  72. #include "Kernel/h/mmMsgTypes.h"
  73.  
  74. /*  These macros define the valid message type and subtype indices */
  75. /*  index values into the Handler structure AFTER they have been   */
  76. /*  extracted from the appropriate message type and subtype        */
  77. /*  fields of a message header.                                    */
  78.  
  79. /* NOTE:  It seems like some of the code will fail if MINMSGTYPE or 
  80.  * MINMSGSUBTYPE are changed to non-zero values.
  81.  */
  82. #define MINMSGTYPE     0
  83. #define MAXMSGTYPE    24
  84. #define MINMSGSUBTYPE  0
  85. #define MAXMSGSUBTYPE 30
  86.  
  87. /*  These macros extract the message type and subtype index     */
  88. /*  from a standard message type and subtype.  Actual types and */
  89. /*  subtypes are assigned by the 'mc' program.                  */
  90. /*  Types are 32-bit integers where the most significant 16     */
  91. /*  bits are the facility code and the least significant 16     */
  92. /*  bits are the version.  Subtypes are 32-bit integers where   */
  93. /*  the most significant 16 bits are the facility code, the     */
  94. /*  next 13 significant bits are the subtype, and the least     */
  95. /*  significant 3 bits are the structure type 'message'.        */
  96.  
  97. #define ExtractType(x) ( ((x) >> 16) - 1 )
  98. #define ExtractSubtype(x) ( ((x) & 0xfff8) >> 3 )
  99.  
  100. typedef struct
  101.     {
  102.         HandlerPtr      Handler;
  103.  
  104.     } HandlerRecord;
  105.  
  106. static HandlerRecord HandlersArray[MAXMSGTYPE-MINMSGTYPE+1]
  107.                                   [MAXMSGSUBTYPE-MINMSGSUBTYPE+1];
  108.  
  109. /************************************************************/
  110. /*                                                          */
  111. /*                   MMDefineMsgHandler                     */
  112. /*                                                          */
  113. /*   MMDefineMsgHandler associates the routine fMsgHandler  */
  114. /*  with the specified message type and subtype and returns */
  115. /*  a pointer to the previously defined handler (or NULL if */
  116. /*  none was defined).                                      */
  117. /*                                                          */
  118. /*  Possible status codes:                                  */
  119. /*      MMSS_Success, MMSK_NoMem, MMSF_BadType,             */
  120. /*      MMSF_BadSubtype.                                    */
  121. /*                                                          */
  122. /************************************************************/
  123.  
  124. KKStatus  MMDefineMsgHandler( fType,      /* Message Type      */
  125.                              fSubtype,   /* Message Subtype   */
  126.                              fMsgHandler, /* Msg Handler        */
  127.               /* returns */  fOldHandler /* Old Handler       */
  128.                            )
  129.   MessageType     fType;
  130.   MessageSubtype  fSubtype;
  131.   HandlerPtr      fMsgHandler;
  132.   HandlerPtr     *fOldHandler;
  133. {
  134.   MessageType    i;
  135.   MessageSubtype j;
  136.  
  137.   MXTraceMsg(3, "MMDefineMsgHandler( 0x%08x, 0x%08x, %d )\n", fType,
  138.                 fSubtype, fMsgHandler);
  139.  
  140.   if ( fType == NULLMSGTYPE )       /* default for ALL messages */
  141.     {   if ( fOldHandler != NULL )
  142.           *fOldHandler = NULL;
  143.         for ( i = MINMSGTYPE; i <= MAXMSGTYPE; i++ )
  144.           for ( j = MINMSGSUBTYPE; j <= MAXMSGSUBTYPE; j++ )
  145.            {
  146.             HandlersArray[i][j].Handler = fMsgHandler;
  147.            }
  148.         return (MMSS_Success);
  149.     }
  150.   i = ExtractType(fType);
  151.   if ( i > MAXMSGTYPE )
  152.     return ( MMSF_BadType);
  153.   if ( fSubtype == NULLMSGSUBTYPE )
  154.     {   if ( fOldHandler != NULL )
  155.           *fOldHandler = NULL;
  156.         for ( j = MINMSGSUBTYPE; j <= MAXMSGSUBTYPE; j++ )
  157.           {
  158.             HandlersArray[i][j].Handler = fMsgHandler;
  159.           }
  160.         return (MMSS_Success);
  161.     };
  162.   j = ExtractSubtype(fSubtype);
  163.   if ( j > MAXMSGSUBTYPE )
  164.     return (MMSF_BadSubtype);
  165.   if ( fOldHandler != NULL )
  166.     *fOldHandler = HandlersArray[i][j].Handler;
  167.  
  168.   HandlersArray[i][j].Handler = fMsgHandler;
  169.   return (MMSS_Success);  
  170. }
  171.  
  172. /************************************************************/
  173. /*                                                          */
  174. /*                   MMRemoveMsgHandler                     */
  175. /*                                                          */
  176. /*   MMRemoveMsgHandler sets the message handler for the    */
  177. /*  specified message type and subtype to be undefined and  */
  178. /*  returns a pointer to the previously defined handler.    */
  179. /*                                                          */
  180. /*  Possible status codes:                                  */
  181. /*      MMSS_Success, MMSF_NoEntry                          */
  182. /*                                                          */
  183. /************************************************************/
  184. #if 0
  185. /*
  186.   This routine has been written as a macro based on the routine
  187.   MMDefineMsgHandler.
  188. */
  189.  
  190. KKStatus  MMRemoveMsgHandlerx( fType,       /* Message Type    */
  191.                               fSubtype,    /* Message Subtype */
  192.                 /* returns */ fOldHandler  /* Old Handler     */
  193.                             )
  194.   MessageType     fType;
  195.   MessageSubtype  fSubtype;
  196.   HandlerPtr     *fOldHandler;
  197. {
  198. }
  199. #endif 0
  200.  
  201. /****************************************************************/
  202. /*                                                              */
  203. /*                       MMDispatchMsg                          */
  204. /*                                                              */
  205. /*   MMDispatchMsg extracts the message type and subtype from  */
  206. /*  the message and calls the message  handler previously       */
  207. /*  identified by the  MMDefineMsgHandler routine.              */
  208. /*                                                              */
  209. /*  Possible status codes:                                      */
  210. /*      MMSS_Success, MMSF_NoEntry, MMSF_BadType,               */
  211. /*      MMSF_BadSubtype.                                        */
  212. /*                                                              */
  213. /****************************************************************/
  214.  
  215. KKStatus  MMDispatchMsg( fMsg       /* Incoming message */
  216.                        )
  217.     MessagePtr fMsg;
  218. {
  219.     HandlerPtr      handler;
  220.     MessageType     type;
  221.     MessageSubtype  subtype;
  222.  
  223.   type = ExtractType(fMsg->MsgHdr.MsgType);
  224.   subtype = ExtractSubtype(fMsg->MsgHdr.MsgSubtype);
  225.  
  226.   MXTraceMsg(5, "MMDispatchMsg( 0x%05x ) type = 0x%06x, subtype = 0x%06x\n",
  227.         fMsg, fMsg->MsgHdr.MsgType, fMsg->MsgHdr.MsgSubtype);
  228.  
  229.   if ( type > MAXMSGTYPE )
  230.     return MMSF_BadType;
  231.   if ( subtype > MAXMSGSUBTYPE )
  232.     return MMSF_BadSubtype;
  233.  
  234.   handler = HandlersArray[type][subtype].Handler;
  235.   if ( handler == NULL )
  236.         return(MMSF_NoEntry);
  237.   else  
  238.     {
  239.         MXTraceMsg(5, "Calling handler 0x%x msg 0x%x\n", handler, fMsg);
  240.         (*handler)(fMsg);
  241.         return(MMSS_Success);
  242.     }
  243. }
  244. /****************************************************************/
  245. /*          End of Message Module Handler Library               */
  246. /****************************************************************/
  247.